package com.yh.csx.crm.core.model;

import com.github.pagehelper.PageInfo;
import com.yh.csx.crm.core.util.BeanUtils;
import lombok.Data;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;


@Data
public class PageList<T> {

    public static <T> PageList<T> of(List<T> list) {
        PageInfo<T> pageInfo = PageInfo.of(list);
        PageList<T> pageList = new PageList<>();
        pageList.pageNum = pageInfo.getPageNum();
        pageList.pageSize = pageInfo.getPageSize();
        pageList.totalCount = pageInfo.getTotal();
        pageList.dataList = pageInfo.getList();
        return pageList;
    }

    public static <T, R> PageList<R> of(List<T> list, Function<T, R> convert) {
        PageInfo<T> pageInfo = PageInfo.of(list);
        PageList<R> pageList = new PageList<>();
        pageList.pageNum = pageInfo.getPageNum();
        pageList.pageSize = pageInfo.getPageSize();
        pageList.totalCount = pageInfo.getTotal();
        pageList.dataList = pageInfo.getList() == null ? null : pageInfo.getList().stream().map(convert).collect(Collectors.toList());
        return pageList;
    }

    public static <T, R> PageList<R> of(List<T> list, Class<R> type, Consumer<R> consumer) {
        return of(list, o -> BeanUtils.convert(type, o, consumer));
    }

    public static <T, R> PageList<R> of(List<T> list, Class<R> type) {
        return of(list, o -> BeanUtils.convert(type, o));
    }

    private int pageNum;
    private int pageSize;
    private long totalCount;
    private List<T> dataList;

    public PageList(int pageNum, int pageSize, int totalCount, List<T> dataList) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.dataList = dataList;
    }

    public PageList() {
    }


}
